package com.timguan.render.web.controller;

import com.timguan.render.api.api.B2CMallServiceReturnCode;
import com.timguan.render.api.entity.cms.CMSPage;
import com.timguan.render.service.common.B2CMallConstants;
import com.timguan.render.service.common.B2CMallServiceConfig;
import com.timguan.render.web.biz.CMSTopicBiz;
import com.timguan.render.web.common.App;
import com.timguan.render.web.common.ServiceExceptionListener;
import com.timguan.render.web.common.TopicNotFoundListener;
import com.timguan.render.web.common.TopicTypeNotMatchListener;
import com.timguan.render.web.common.ContextFilter;
import net.pocrd.entity.ServiceRuntimeException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;

/**
 * 专题访问入口
 */
@Controller
public class ActivityController extends BaseController {
    @Autowired
    private TopicNotFoundListener topicNotFoundListener;
    @Autowired
    private TopicTypeNotMatchListener topicTypeNotMatchListener;
    @Autowired
    private CMSTopicBiz cmsTopicBiz;

    /**
     * 异常监听注册
     */
    @Override
    public void initServiceExceptionListener() {
        setServiceExceptionListeners(new ArrayList<ServiceExceptionListener>() {
            {
                add(topicNotFoundListener);
            }

            {
                add(topicTypeNotMatchListener);
            }
        });
    }

    @RequestMapping("/{uri}.html")
    public ModelAndView getActivityWithURI(
            @PathVariable(value = "uri")
            String uri) {
        App app = ContextFilter.RequestContext.getCurrent().getApp();
        String appVersion = ContextFilter.RequestContext.getCurrent().getAppVersion();
        CMSPage cmsPage = cmsTopicBiz.getCmsPage(uri, app, appVersion);
        return buildModelAndView(cmsPage.getPageId(), app);
    }

    /**
     * 通过topicId获取topic相关的信息
     *
     * @param id
     */
    @RequestMapping("/activity/{id}.html")
    public ModelAndView getActivityWithId(
            @PathVariable(value = "id")
            String id) {
        if (StringUtils.isNotEmpty(id) && !StringUtils.isNumeric(id)) {
            throw new ServiceRuntimeException(B2CMallServiceReturnCode.TOPIC_NOT_FOUND, "未知专题，id:" + id);
        }
        App app = ContextFilter.RequestContext.getCurrent().getApp();
        String appVersion = ContextFilter.RequestContext.getCurrent().getAppVersion();
        //查询topic信息
        CMSPage cmsPage = cmsTopicBiz.getCmsPage(Integer.parseInt(id), app, appVersion);
        return buildModelAndView(cmsPage.getPageId(), app);
    }

    private ModelAndView buildModelAndView(int pageId, App app) {
        ModelAndView modelAndView = new ModelAndView();
        //获取页面数据
        modelAndView.addObject(B2CMallConstants.CMS_COMMON_DATA, cmsTopicBiz.getDataOfPage(pageId, app));
        //设置资源访问前缀
        CMSTopicBiz.Resources cmsTopicBizResources = cmsTopicBiz.getResources(pageId);
        modelAndView.addObject(B2CMallConstants.CMS_COMMON_HEADER, cmsTopicBizResources.getHeader());
        modelAndView.addObject(B2CMallConstants.CMS_COMMON_FOOTER, cmsTopicBizResources.getFooter());
        modelAndView.addObject(B2CMallConstants.CMS_CDN_DOMAIN_KEY, B2CMallServiceConfig.getInstance().getCdnDomain());
        if (App.B2CPC != app) {
            modelAndView.addObject(B2CMallConstants.CMS_COMMON_CSS_KEY, cmsTopicBizResources.getH5commoncss());
            modelAndView.addObject(B2CMallConstants.CMS_COMMON_JS_KEY, cmsTopicBizResources.getH5commonjs());
        } else {
            modelAndView.addObject(B2CMallConstants.CMS_COMMON_CSS_KEY, cmsTopicBizResources.getPccommoncss());
            modelAndView.addObject(B2CMallConstants.CMS_COMMON_JS_KEY, cmsTopicBizResources.getPccommonjs());
        }
        modelAndView.setViewName(String.valueOf(pageId));
        return modelAndView;
    }
}
